package com.happy.xiao.common.redis;


import com.happy.xiao.common.redis.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/1 16:00.
 */
@Service
public class RedisServiceImpl implements IRedisService {

    @Autowired
    protected RedisTemplate<String, Object> redisTemplate;

    @Resource
    protected HashOperations<String, String, String> hashOperations;

    @Resource
    protected HashOperations<String, String, Object> hashOperationsObj;

    @Resource
    protected ValueOperations<String, Object> valueOperations;

    /**
     * 添加
     *
     * @param key    key
     * @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间
     */
    public void put(String hashKey, String key, String values, long expire) {

        hashOperations.put(hashKey, key, values);
        if (expire != -1) {
            redisTemplate.expire(hashKey, expire, TimeUnit.SECONDS);
        }
    }

    public void putObj(String hashKey, String key, Object values, long expire) {

        hashOperationsObj.put(hashKey, key, values);
        if (expire != -1) {
            redisTemplate.expire(hashKey, expire, TimeUnit.SECONDS);
        }
    }

    public void setKeyValue(String key, Object values, long expire) {
        valueOperations.set(key, values);
        if (expire != -1) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void setKeyValue(String key, Object values) {
        valueOperations.set(key, values);
    }

    /**
     * 删除
     *
     * @param key 传入key的名称
     */
    public void delete(String hashKey, String key) {
        hashOperations.delete(hashKey, key);
    }

    /**
     * 查询
     *
     * @param key 查询的key
     * @return
     */
    public String get(String hashKey, String key) {
        return hashOperations.get(hashKey, key);
    }

    public Object getObj(String hashKey, String key) {
        return hashOperationsObj.get(hashKey, key);
    }

    public Object getKeyValue(String key) {
        return valueOperations.get(key);
    }

    /**
     * 获取当前redis库下所有对象
     *
     * @return
     */
    public List<String> getAll(String hashKey) {
        return hashOperations.values(hashKey);
    }

    /**
     * 查询查询当前redis库下所有key
     *
     * @return
     */
    public Set<String> getKeys(String hashKey) {
        return hashOperations.keys(hashKey);
    }

    /**
     * 判断key是否存在redis中
     *
     * @param key 传入key的名称
     * @return
     */
    public boolean isKeyExists(String hashKey, String key) {
        return hashOperations.hasKey(hashKey, key);
    }

    /**
     * 查询当前key下缓存数量
     *
     * @return
     */
    public long count(String hashKey) {
        return hashOperations.size(hashKey);
    }

    /**
     * 清空redis
     */
    public void empty(String hashKey) {
        Set<String> set = hashOperations.keys(hashKey);
        set.stream().forEach(key -> hashOperations.delete(hashKey, key));
    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);

    }

    /**
     * 根据前缀key模糊删除
     *
     * @param prex
     */
    public void deleteByPrex(String prex) {
        Set<String> keys = redisTemplate.keys(prex + "*");
        redisTemplate.delete(keys);
    }

    /**
     * 根据后缀key模糊删除
     *
     * @param suffix
     */
    public void deleteBySuffix(String suffix) {
        Set<String> keys = redisTemplate.keys("*" + suffix);
        redisTemplate.delete(keys);
    }

    /**
     * 加减
     * @param key
     * @param count
     */
    @Override
    public void increment(String key, long count) {
        if (redisTemplate.hasKey(key)) {
            valueOperations.increment(key, count);
        } else {
            valueOperations.set(key, 1);
        }
    }

    @Override
    public void increment(String key, long count, int delayTime) {

        if (redisTemplate.hasKey(key)) {
            valueOperations.increment(key, count);
        } else {
            valueOperations.set(key, 1);
        }
        redisTemplate.expire(key, delayTime, TimeUnit.SECONDS);
    }
}